function [ output_args ] = plsr(ncomp)

[folder_path] = uigetdir('../stats', 'Load Wrist Stats Data');
sub_folder = dir(folder_path);

count = 0;
for p = 3 : length(sub_folder)
    file_name = dir(fullfile(folder_path, sub_folder(p).name, '*.csv'));
    file_path = fullfile(folder_path, sub_folder(p).name);
    for i = 1 : length(file_name)
        count = count + 1;
        
        wrist_data =  csvread(fullfile(file_path, file_name(i).name));
        
        time_step = wrist_data(:,1);
        weight = wrist_data(:,2);
        mu = wrist_data(:,3);
        force = wrist_data(:,4:6);
        torque = wrist_data(:,7:9);
        pos = wrist_data(:,10:12);
        quat = wrist_data(:,13:16);
        
        weight_collection(count, 1:size(weight, 1), 1:size(weight, 2)) = weight;
        mu_collection(count, 1:size(mu, 1), 1:size(mu, 2)) = mu;
        force_collection(count, 1:size(force, 1), 1:size(force, 2)) = force;
        torque_collection(count, 1:size(torque, 1), 1:size(torque, 2)) = torque;
        pos_collection(count, 1:size(pos, 1), 1:size(pos, 2)) = pos;
        quat_collection(count, 1:size(quat, 1), 1:size(quat, 2)) = quat;
    end
end

file_path = fullfile(file_path, '..', 'plsr');
if ~exist(file_path, 'dir')
    mkdir(file_path);
end

X = zeros(size(force_collection, 1), size(wrist_data, 2)-3);
Y = zeros(size(force_collection, 1), 2);
for i = 1 : size(time_step, 1)
    for j = 1 : count
        force_obs(1,:) = force_collection(j, i, :);
        torque_obs(1,:) = torque_collection(j, i, :);
        pos_obs(1,:) = pos_collection(j, i, :);
        quat_obs(1,:) = quat_collection(j, i, :);
        X(j, :) = [force_obs torque_obs pos_obs quat_obs];
        
        weight_obs(1,:) = weight_collection(j, i, :);
        mu_obs(1,:) = mu_collection(j, i, :);
        Y(j, :) = [weight_obs mu_obs];
    end
    
    X = zscore(X);
    Y = zscore(Y);
    
    [XL, YL, XS, YS, BETA, PCTVAR] = plsregress(X, Y, ncomp);
    plot(1:ncomp,cumsum(100*PCTVAR(2,:)),'-bo');
    xlabel('Number of PLS components');
    ylabel('Percent Variance Explained in y');
    
    csvwrite(fullfile(file_path, [num2str(i) '.csv']), BETA);
end

% X = zeros(size(force_collection, 1), 7);
% Y = zeros(size(force_collection, 1), 2);
% for i = 1 : size(time_step, 1)
%     for j = 1 : count
%         pos_obs(1,:) = pos_collection(j, i, :);
%         quat_obs(1,:) = quat_collection(j, i, :);
%         X(j, :) = [pos_obs quat_obs];
%         
%         weight_obs(1,:) = weight_collection(j, i, :);
%         mu_obs(1,:) = mu_collection(j, i, :);
%         Y(j, :) = [weight_obs mu_obs];
%     end
%     [X, Xmu, Xsigma] = zscore(X);
%     [Y, Ymu, Ysigma] = zscore(Y);
%     [XL, YL, XS, YS, BETA, PCTVAR] = plsregress(X, Y, ncomp);
% %     plot(1:7,cumsum(100*PCTVAR(2,:)),'-bo');
% %     xlabel('Number of PLS components');
% %     ylabel('Percent Variance Explained in y');
%     
%     csvwrite(fullfile(file_path, [num2str(i) '.csv']), BETA);
% end
end



